//
//  NetworkTools.swift
//  weibo_swift01
//
//  Created by 孙郝聪 on 16/8/5.
//  Copyright © 2016年 shc. All rights reserved.
//

import UIKit
import AFNetworking

//HTTP请求方法枚举
enum HMRequestMethod: String{
    case GET = "GET"
    case POST = "POST"
}

class NetworkTools: AFHTTPSessionManager {

    //MARK: - 应用程序信息

    private let appKey = "4182500062"
    private let appSecret = "5fc0814c2ec672633f4fc5e399d5dabb"
    private let redirectUrl =  "http://weibo.com/u/1690764110/home?wvr=5"



    //单例
    static let sharedTools: NetworkTools = {
        let tools = NetworkTools(baseURL: nil)

        //设置反序列化数据格式  -  系统会自动将oc 框架中的NSSet 转换set

        tools.responseSerializer.acceptableContentTypes?.insert("text/html")

        return tools
    }()

    func request(method: HMRequestMethod,URLString: String, parameters: [String:AnyObject]?,finished:(result: AnyObject?,error: NSError?)->()){

        //定义成功回调
        let success = { (task: NSURLSessionDataTask, result: AnyObject?) -> Void in
            finished(result: result, error: nil)
        }
        //定义失败的回调
        let failure = {(task:NSURLSessionDataTask?,error:NSError) -> Void in
            //在开放网络应用的时候 错误不要提示给用户  但是错误一定要输出
            print(error)
            finished(result: nil, error: error)
        }

        if method == HMRequestMethod.GET {
            GET(URLString, parameters: parameters, success: success, failure: failure)
        } else {
            POST(URLString, parameters: parameters, success: success, failure: failure)
        }   

    }

}
